package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.model.BindVO;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryRelationVO;
import com.nodecollege.cloud.common.model.po.UpmsMenu;
import com.nodecollege.cloud.common.model.po.UpmsRole;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.MenuService;
import com.nodecollege.cloud.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单管理
 *
 * @author LC
 * @date 2020/1/3 23:02
 */
@RestController
@UserLoginAnnotation
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @ApiAnnotation(modularName = "菜单管理", description = "查询菜单树")
    @PostMapping("/getMenuTree")
    public NCResult getMenuTree(@RequestBody UpmsMenu upmsMenu) {
        upmsMenu.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        upmsMenu.setMenuSource(NCConstants.INT_1);
        return NCResult.ok(menuService.getMenuTree(upmsMenu));
    }

    @ApiAnnotation(modularName = "角色管理", description = "查询菜单拥有角色列表")
    @PostMapping("/getRoleListByMenuId")
    public NCResult getMenuListByRoleId(@RequestBody QueryRelationVO<UpmsRole, UpmsMenu> queryVO) {
        queryVO.getEndNode().setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        return NCResult.ok(roleMenuService.getRoleListByMenuId(queryVO));
    }

    @ApiAnnotation(modularName = "角色管理", description = "绑定/解绑角色成员关系")
    @PostMapping("/bindRoleMenuByMenuId")
    public NCResult bindRoleMenuByRoleId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(false);
        roleMenuService.bindRoleMenu(bindVO);
        return NCResult.ok();
    }
}
